package com.dunqi.gpm.cangxi.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.cangxi.home.adapter.UpAdapter;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.Up;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/12/21 0021.
 */

public class UpListFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    long projectId;
    ArrayList<Up> data = new ArrayList<>();
    private UpAdapter adapter;


    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();

    }

    private void net() {
        ApiFactory.getInstance()
                .queryProjectAdmireList(projectId, page)
                .compose(RxSchedulers.<Page<Up>>io_main())
                .subscribe(new Consumer<Page<Up>>() {
                    @Override
                    public void accept(Page<Up> pages) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);

                        ArrayList<Up> list = pages.getList();

                        if (page==1) {
                            adapter.replaceData(list);
                        }else {
                            adapter.addData(list);
                        }

                        if (pages.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        }else {
                            adapter.loadMoreEnd(true);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        CommonUtil.showSingleToast("获取失败");
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);


        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new UpAdapter(data);
        recycler.setAdapter(adapter);

        recycler.addItemDecoration(new RecyclerItemDecoration(1,RecyclerItemDecoration.VERTICAL));

        adapter.setEmptyView(new EmptyView(context));

        adapter.setOnLoadMoreListener(this, recycler);


        swipeRefresh.setOnRefreshListener(this);


    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        projectId = arguments.getLong(Constance.PROJECTID, -1);
    }

    public static UpListFragment newInstance(long projectId) {
        UpListFragment fragment = new UpListFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECTID, projectId);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_up_list;
    }
}
